
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
#include <string.h>
#include <sys/ioctl.h>
#include <stdlib.h>
#include <unistd.h> 


#define  LCD_PWM_POWER_ON_OFF_GPIO					46
#define  CMD_LCD_PWM_POWER_ON_OFF_GPIO_EXPORT			"echo 46 > /sys/class/gpio/export"
#define  CMD_LCD_PWM_POWER_ON_OFF_GPIO_UNEXPORT			"echo 46 > /sys/class/gpio/unexport"
#define  LCD_PWM_POWER_ON_OFF_GPIO_DIR					"/sys/class/gpio/gpio46/direction"
#define  CMD_LCD_PWM_POWER_ON_OFF_GPIO_DIR_SET_OUT		"echo out > /sys/class/gpio/gpio46/direction"
#define  LCD_PWM_POWER_ON_OFF_GPIO_VAL					"/sys/class/gpio/gpio46/value"


static int is_file_exits( const char *filepath)
{
	if( !filepath)	return -1;
    if( access( filepath ,F_OK) < 0)   
    {
		return -1;
    }
	return 1;
}



static int lcd_pwm_gpio_get_value( int gpio)
{
    char value_file[128] = { 0};
    int value_fd;
    int size;
    char value_str[64] = {0};
	int gpio_value = 0;

	
    sprintf(value_file,"/sys/class/gpio/gpio%d/value", gpio);
    value_fd = open(value_file, O_RDWR);
    if(value_fd <= 0){
        printf("lcd_pwm_gpio_get_value, open file %s failed\n", value_file);
        return -1;
    }

    size = read(value_fd, value_str, 1); 
    if(size <= 0){
        printf("lcd_pwm_gpio_get_value, read gpio %s failed\n", value_file);
        close(value_fd);
        return -1;
    }
    close(value_fd);

	gpio_value = atoi( value_str);

//	printf("lcd_pwm_gpio_get_value,gpio_get_value[ %d] : %d\n", gpio, gpio_value);

    return gpio_value;
}

static int lcd_pwm_gpio_set_value( int gpio, int value)
{
    char value_file[128] = { 0};
	int size;

    sprintf(value_file,"/sys/class/gpio/gpio%d/value", gpio);
    int value_fd = open(value_file, O_RDWR);
    if(value_fd <= 0){
        printf("lcd_pwm_gpio_set_value open file %s failed, %s\n", value_file, strerror(errno));
        return -1;
    }

    size = write(value_fd, value == 0 ? "0" : "1", 1);
    if(size <= 0){
        printf("lcd_pwm_gpio_set_value write  %s failed, %s\n", value_file, strerror(errno));
        close(value_fd);
        return -1;
    }
    close(value_fd);
    return 0;
}


static int lcd_pwm_gpio_get_dir( char *dir)
{
    int value_fd;
    int size;
    char value_str[64] = {0};

    value_fd = open( LCD_PWM_POWER_ON_OFF_GPIO_DIR, O_RDWR);
    if(value_fd <= 0){
        printf("lcd_pwm_gpio_get_dir open file %s failed\n", LCD_PWM_POWER_ON_OFF_GPIO_DIR);
        return -1;
    }

    size = read( value_fd, value_str, 10); 
    if(size <= 0){
        printf("lcd_pwm_gpio_get_dir read gpio %s failed\n", LCD_PWM_POWER_ON_OFF_GPIO_DIR);
        close(value_fd);
        return -1;
    }
    close(value_fd);

	strcpy( dir, value_str);
//	printf("lcd_pwm_gpio_get_dir: %s\n", dir);

    return 0;
}




int lcd_power_enable( int enable)
{
	static int first_used = 0;
	int gpio_value = 0;
	int ret;
	char direction[10] = { 0};

	if( (enable < 0 ) || (enable > 1)) {
		printf("lcd_power_on_off error parameter, %d\n", enable);
		return -1;
	}
    ret = is_file_exits( LCD_PWM_POWER_ON_OFF_GPIO_VAL);
    if (ret < 0) {
        printf("lcd_power_on_off, gpio export.\n");
		printf("lcd_power_on_off init.\n");
		system( "echo 46 > /sys/class/gpio/export");
		system( "chmod 777 /sys/class/gpio/gpio46/*");
		usleep(100);
    }

    gpio_value = lcd_pwm_gpio_get_value( LCD_PWM_POWER_ON_OFF_GPIO);
    if ( gpio_value < 0) {
		printf("lcd_power_on_off fail to get gpio value: %d\n", gpio_value);
		return -1;
    }

    if( gpio_value != enable) {

		ret = lcd_pwm_gpio_get_dir( direction);
  		if(ret < 0) {
  			printf("lcd_pwm_gpio_get_dir fail\n");
  			return -1;
  		}
    	if( !strstr( direction, "out")) {
    		printf("lcd_pwm_gpio_get_dir, gpio set out mode.\n");
    		system( CMD_LCD_PWM_POWER_ON_OFF_GPIO_DIR_SET_OUT);
    		usleep(100);
    	}

    	lcd_pwm_gpio_set_value( LCD_PWM_POWER_ON_OFF_GPIO, enable);
    	return 0;
    }

	return 0;
}



